//
//  FloatWindow.m
//  Common
//
//  Created by shiqing guo on 2018/10/10.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "FloatWindow.h"
#import "Macro.h"
#import "UIView+DDAddition.h"

//#define kAssistiveTouchIMG    [UIImage imageNamed:@"trip_plus60"]
#define kPrompt_DismisTime    0.2
#define kAlpha                0.5
#define kAllowance            30.0f

@interface FloatWindow ()
@property (nonatomic, strong) UIButton *btnTouch;
@end

@implementation FloatWindow

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        
        self.backgroundColor = [UIColor clearColor];
        self.windowLevel = UIWindowLevelAlert + 1;
        [self makeKeyAndVisible];
        
        [self addSubview:self.btnTouch];
    }
    return self;
}

#pragma mark - Lazy
- (UIButton *)btnTouch
{
    if (!_btnTouch) {
        _btnTouch = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnTouch.frame = CGRectMake(0, 0, self.width, self.height);
        [_btnTouch addTarget:self action:@selector(floatWindowTouchAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnTouch;
}

- (void)setImageName:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    [self.btnTouch setBackgroundImage:image forState:UIControlStateNormal];
    [self.btnTouch setBackgroundImage:image forState:UIControlStateDisabled];
    [self.btnTouch setBackgroundImage:image forState:UIControlStateHighlighted];
    [self setNeedsDisplay];
}

#pragma mark - private action
- (void)floatWindowTouchAction:(id)sender
{
    WEAKSELF
    [UIView animateWithDuration:kPrompt_DismisTime animations:^{
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [kWindow endEditing:YES];
    }];
    if (self.clickBlock) self.clickBlock();
}

- (void)setAlpha {
    [UIView animateWithDuration:kPrompt_DismisTime animations:^{
        self.alpha = kAlpha;
    }];
}

- (void)setShow {
    [UIView animateWithDuration:kPrompt_DismisTime animations:^{
        self.alpha = 1.0;
    }];
}

- (void)setHide {
    [UIView animateWithDuration:kPrompt_DismisTime animations:^{
        self.alpha = 0;
    }];
}
@end
